/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mum.edu.cs.waa.assignment.controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mum.edu.cs.waa.assignment.model.Account;
import mum.edu.cs.waa.assignment.model.Database;

/**
 *
 * @author teke
 */
@WebServlet(name = "login", urlPatterns = {"/login"})
public class login extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = "index.jsp";
        String btn = request.getParameter("btn");
        if (!btn.equalsIgnoreCase("Back")) {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            Database db = (Database) getServletContext().getAttribute("Database");
            Account acc = db.GetAccount(username, password);
            if (acc != null) {
                request.getSession().setAttribute("Accunt", acc);
                page = "post.jsp";
            } else {
                page = "loginfail.jsp";
            }
        }

        RequestDispatcher topost = request.getRequestDispatcher(page);
        topost.forward(request, response);
    }
}
